package org.tpdata.api.singlequality.hospitalqc;

import business.inhospitalqc.LaunchQc;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.Login;
import common.RunCaseJson;
import exceptions.TestNGRetry;
import io.qameta.allure.Feature;
import io.restassured.response.Response;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

import common.Data;

@Feature("发起质控指派")
public class LaunchQcAssign {
    Logger log = Logger.getLogger(LaunchQcAssign.class);
    LaunchQc launchQc=new LaunchQc();
    Data data= Login.login();

    public Map beforetest(){
        String modleurl = "api-sourcedata/dmp/template/create";
        String ModelResulturl = "api-sourcedata/dmp/template/result/create";
        String PatientListurl = "api-sourcedata/dmp/template/result/list";
        String BatchCreateurl = "api-sourcedata/qc/batch/batchCreate.json";
        String BatchPatientListurl = "api-sourcedata/qc/batch/patient/list.json";
        String UserListurl = "api-users/user/list";
//        String url = "api-sourcedata/qc/task/create.json";
        String templateId =launchQc.CreateModel(modleurl);
        launchQc.CreateModelResult(templateId,ModelResulturl);
        String inpatientNo =launchQc.GetPatientList(templateId, PatientListurl);
        String batchId = launchQc.BatchCreate(templateId, inpatientNo, BatchCreateurl);
        String ids = launchQc.BatchGetPatientList(batchId, BatchPatientListurl);
        String practitionerId = launchQc.GetUserList(UserListurl);
        Map<String,String> map=new HashMap<>();
        map.put("batchId",batchId);
        map.put("ids",ids);
        map.put("practitionerId",practitionerId);   //医生ID
        return map;
    }

    @Parameters({"diseaseCode","qcCheckOption"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "")
    public void CreateTask( String diseaseCode,String qcCheckOption ) {
        Map map=beforetest();
        String url = "api-sourcedata/qc/task/create.json";
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("type", "1");   //1=copd，2=aecopd，3=指标检测图
        json.put("practitionerId", map.get("practitionerId"));   //从api-users/user/list获取userId
        json.put("qcCheckOption", qcCheckOption);    //质量管理措施项目 1、急诊记录 2、入院24H内 3、入院72H内 4、治疗72H之后 5、出院前1~2周 6、出院日

        json.put("batchId", map.get("batchId"));                  //从/qc/batch/batchCreate.json 获取id
        json.put("ids", map.get("ids"));    //从/qc/batch/patient/list.json 获取id
        json.put("diseaseCode", diseaseCode);
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "post", url);
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String responseResult = jsonObject.getString("responseResult");
        Assert.assertEquals(responseResult,"SUCCESS");
    }

}
